﻿/*
    Copyright (C) 2008, wj2929, wj2929@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using mjRegister.Entity;
using System.Net;
using System.IO;

namespace mjRegister.BAL
{
    //汽车
    public class Park
    {
        static RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);

        //获取汽车数据（几辆车、停车收入、总资产、当前现金等）
        public static ParkInfo GetParkInfoByUid(string verify, string uid)
        {
            string url = string.Format("http://www.kaixin001.com/parking/usercar.php?verify={0}&puid={1}",verify,uid);
            HttpProc hp = new HttpProc(url);
            string ResStr1 = hp.Proc();
            ParkInfo parkInfo = new ParkInfo();
            parkInfo.Verify = verify;
            parkInfo.ParkAmountInfo = new ParkAmountInfo();
            string re = "\\\"cash\\\":(\")?(?<cash>\\d+)(\")?";
            if (Regex.IsMatch(ResStr1, re))
                parkInfo.ParkAmountInfo.Cash = int.Parse(Regex.Match(ResStr1, re).Result("${cash}"));
            string re1 = @"\{\""carid\"":\""(?<carid>\d+)\"",\""price\"":\""(?<price>\d+)\"",\""neighbor\"":\""(?<neighbor>\d+)\"",\""park_uid\"":(\""|\s*)(?<park_uid>\d+)(\""|\s*),\""parkid\"":(\""|\s*)(?<parkid>\d+)(\""|\s*),\""ctime\"":(?<ctime>\d+),\""car_name\"":\""(?<car_name>.*?)\"",\""car_logo_big\"":\""(?<car_logo_big>.*?)\"",\""car_logo_small\"":\""(?<car_logo_small>.*?)\"",\""car_logo_flash\"":\""(?<car_logo_flash>.*?)\"",\""car_trademark\"":\""(?<car_trademark>.*?)\"",\""car_trademarkname\"":\""(?<car_trademarkname>.*?)\"",\""car_police\"":\""(?<car_police>\d+)\"",\""park_real_name\"":\""(?<park_real_name>.*?)\"",\""park_logo20\"":(\""|\s*)(?<park_logo20>.*?)(\""|\s*),\""park_profit\"":(?<park_profit>\d+),\""park_moneyminute\"":(\""|\s*)(?<park_moneyminute>.*?)(\""|\s*)\}";
            if (Regex.IsMatch(ResStr1, re1))
            {
                MatchCollection mc = Regex.Matches(ResStr1, re1);
                for (int i = 0;i< mc.Count; i++)
                {
                    Match match = mc[i];
                    CarInfo carInfo = new CarInfo
                    {
                        carid = int.Parse(match.Result("${carid}")),
                        price = int.Parse(match.Result("${price}")),
                        neighbor = match.Result("${neighbor}"),
                        park_uid = int.Parse(match.Result("${park_uid}")),
                        parkid = int.Parse(match.Result("${parkid}")),
                        ctime = match.Result("${ctime}"),
                        car_name = Common.utf8(match.Result("${car_name}")),
                        car_logo_big = Regex.Unescape(match.Result("${car_logo_big}")),
                        car_logo_small = Regex.Unescape(match.Result("${car_logo_small}")),
                        car_logo_flash = Regex.Unescape(match.Result("${car_logo_flash}")),
                        car_trademark = Regex.Unescape(match.Result("${car_trademark}")),
                        car_police = match.Result("${car_police}"),
                        park_real_name = Regex.Unescape(match.Result("${park_real_name}")),
                        park_logo20 = Regex.Unescape(match.Result("${park_logo20}") == "null" ? string.Empty : match.Result("${park_logo20}")),
                        park_profit = int.Parse(match.Result("${park_profit}")),
                        park_moneyminute = int.Parse(match.Result("${park_moneyminute}") == "null" ? "0" : match.Result("${park_moneyminute}"))
                    };
                    parkInfo.ParkAmountInfo.AllCarAmount += carInfo.price;
                    parkInfo.CarInfos.Add(carInfo);
                }
                parkInfo.ParkAmountInfo.TotalAmount = parkInfo.ParkAmountInfo.AllCarAmount + parkInfo.ParkAmountInfo.Cash;
                return parkInfo;
            }
            else
                return parkInfo;

        }

        //送车
        public static string SendCar(string verify, string carid, string touid, string rcode, string code,CookieCollection cc)
        {
            string postData = string.Format("verify={0}&carid={1}&touid={2}&rcode={3}&code={4}", verify, carid, touid, rcode, code);
            string url = "http://www.kaixin001.com/parking/givefriend.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/givefriend.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                if(Regex.IsMatch(ResStr1,re,options))
                {
                    Match mc = Regex.Match(ResStr1,re,options);
                    string matchcontent = mc.Value;
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        //卖车
        public static string CardRecycle(string verify, string cardid, string carid)
        {
            string postData = string.Format("verify={0}&cardid={1}&carid={2}", verify, cardid, carid);
            string url = "http://www.kaixin001.com/parking/card_recycle.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/card_recycle.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div.*?style=\\\"display:block\\\">(?<block>.*?)</div>\\s+</div>\\s+</div";
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Result("${block}");
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        //升级场景
        public static string CardScene(string verify, string cardid)
        {
            string postData = string.Format("verify={0}&cardid={1}", verify, cardid);
            string url = "http://www.kaixin001.com/parking/card_scene.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/card_scene.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div.*?style=\\\"display:block\\\">(?<block>.*?)</div>\\s+</div>\\s+</div";
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Result("${block}");
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }


        //免费升级场景
        public static string CardFreeScene(string verify, string cardid)
        {
            string postData = string.Format("verify={0}&cardid={1}", verify, cardid);
            string url = "http://www.kaixin001.com/parking/card_free_scene.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/card_free_scene.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div.*?style=\\\"display:block\\\">(?<block>.*?)</div>\\s+</div>\\s+</div";
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Result("${block}");
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        //买卡
        public static string BuyCard(string verify, string cardid)
        {
            string postData = string.Format("verify={0}&cardid={1}", verify, cardid);
            string url = "http://www.kaixin001.com/parking/buycard.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/buycard.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }


        //换车
        public static string UpdateCar(string verify, string oldcarid, string carid, string color)
        {
            string postData = string.Format("verify={0}&carid={1}&oldcarid={2}&color={3}", verify, carid, oldcarid, color);
            string url = "http://www.kaixin001.com/parking/updatecar.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/updatecar.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        //买车
        public static string PurchaseCar(string verify, string carid, string color)
        {
            string postData = string.Format("verify={0}&carid={1}&color={2}", verify, carid, color);
            string url = "http://www.kaixin001.com/parking/purchase.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/purchase.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    string re1 = "<strong>(?<message>.*?)</strong>";
                    return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;

        }

        //举报
        public static string DoCall(string acc, string verify, string park_uid, string parkid)
        {
            string postData = string.Format("acc={0}&verify={1}&park_uid={2}&parkid={3}",
                acc,  //开心网防外挂校验参数……
                verify, park_uid, parkid);
            string url = "http://www.kaixin001.com/parking/call.php";
            HttpProc hp = new HttpProc(url,postData);
            string ResStr1 = hp.Proc();
            string re = "\\\"error\\\":\\\"(?<error>.*?)\\\"";
            string message = string.Empty;
            if (Regex.IsMatch(ResStr1, re))
                message = Common.utf8(Regex.Match(ResStr1, re).Result("${error}"));
            return message;
        }
    }
}
